---
title: Syntax Comparison Basics
---

This module delves into the fundamental syntax differences between JavaScript and C++, covering variable declarations, control flow, functions, and basic data types.

## Variable Declaration and Type System

JavaScript is dynamically typed, meaning variable types are determined at runtime. C++ is statically typed, requiring explicit type declarations at compile time.

<UniversalEditor title="Variable Declaration and Assignment Comparison" compare={true}>
```javascript !! js
// JavaScript: Dynamic Typing
let age = 30; // Number
let name = "Alice"; // String
let isActive = true; // Boolean

console.log(typeof age); // "number"
console.log(typeof name); // "string"
```

```cpp !! cpp
// C++: Static Typing
int age = 30; // Integer
std::string name = "Alice"; // String
bool isActive = true; // Boolean

// C++ types are checked at compile time, no 'typeof' equivalent for runtime type checking in this manner.
// To print values:
// std::cout << "Age: " << age << std::endl;
// std::cout << "Name: " << name << std::endl;
```
</UniversalEditor>

## Control Flow Statements Comparison

Both languages support common control flow structures like `if/else`, `for`, `while`, and `switch`, but with minor syntactic differences.

### If/Else Statements

<UniversalEditor title="If/Else Comparison" compare={true}>
```javascript !! js
// JavaScript: If/Else
let temperature = 25;

if (temperature > 30) {
  console.log("It's hot!");
} else if (temperature > 20) {
  console.log("It's warm.");
} else {
  console.log("It's cold.");
}
```

```cpp !! cpp
// C++: If/Else
int temperature = 25;

if (temperature > 30) {
  // std::cout << "It's hot!" << std::endl;
} else if (temperature > 20) {
  // std::cout << "It's warm." << std::endl;
} else {
  // std::cout << "It's cold." << std::endl;
}
```
</UniversalEditor>

### For Loops

<UniversalEditor title="For Loop Comparison" compare={true}>
```javascript !! js
// JavaScript: For Loop
for (let i = 0; i < 5; i++) {
  console.log(i);
}

// For...of (for iterables)
let arr = [1, 2, 3];
for (let val of arr) {
  console.log(val);
}
```

```cpp !! cpp
// C++: For Loop
for (int i = 0; i < 5; i++) {
  // std::cout << i << std::endl;
}

// Range-based for loop (C++11 and later)
std::vector<int> vec = {1, 2, 3};
for (int val : vec) {
  // std::cout << val << std::endl;
}
```
</UniversalEditor>

## Function Definition and Calling

Functions in C++ require explicit return types and parameter types, unlike JavaScript.

<UniversalEditor title="Function Definition Comparison" compare={true}>
```javascript !! js
// JavaScript: Function Definition
function greet(name) {
  return `Hello, ${name}!`;
}

const add = (a, b) => a + b; // Arrow function

console.log(greet("Bob"));
console.log(add(5, 3));
```

```cpp !! cpp
// C++: Function Definition
#include <string> // Required for std::string

std::string greet(std::string name) {
  return "Hello, " + name + "!";
}

int add(int a, int b) {
  return a + b;
}

// To call in main function:
// std::cout << greet("Bob") << std::endl;
// std::cout << add(5, 3) << std::endl;
```
</UniversalEditor>

## Scope and Lifetime

Both languages have block scope, but C++ variables have a more explicit lifetime tied to their scope.

<UniversalEditor title="Scope and Lifetime Comparison" compare={true}>
```javascript !! js
// JavaScript: Scope
function example() {
  let x = 10;
  if (true) {
    let y = 20;
    console.log(x); // 10
    console.log(y); // 20
  }
  // console.log(y); // ReferenceError: y is not defined
}
example();
```

```cpp !! cpp
// C++: Scope
#include <iostream>

void example() {
  int x = 10;
  if (true) {
    int y = 20;
    // std::cout << x << std::endl; // 10
    // std::cout << y << std::endl; // 20
  }
  // std::cout << y << std::endl; // Error: 'y' was not declared in this scope
}
// Call example() in main
```
</UniversalEditor>

## Basic Data Type Comparison

C++ has a richer set of primitive data types with fixed sizes, offering more control over memory.

| JavaScript Type | C++ Equivalent(s)                                  |
| :-------------- | :------------------------------------------------- |
| `number`        | `int`, `float`, `double`, `long`, `short`, etc.    |
| `string`        | `std::string`, `char[]`, `char*`                   |
| `boolean`       | `bool`                                             |
| `null`          | `nullptr` (C++11), `NULL` (C-style)                |
| `undefined`     | No direct equivalent; often represented by `nullptr` or specific logic |
| `object`        | `class`, `struct`, `union`                         |
| `symbol`        | No direct equivalent                               |
| `bigint`        | `long long` (for larger integers), external libraries for arbitrary precision |

## Operators and Expressions

Most arithmetic, comparison, and logical operators are similar, but C++ has additional operators for pointers and memory management.

<UniversalEditor title="Operators Comparison" compare={true}>
```javascript !! js
// JavaScript: Operators
let a = 10, b = 3;
let sum = a + b; // 13
let isEqual = (a === b); // false (strict equality)
let isGreater = (a > b); // true
```

```cpp !! cpp
// C++: Operators
int a = 10, b = 3;
int sum = a + b; // 13
bool isEqual = (a == b); // false (equality)
bool isGreater = (a > b); // true

// C++ also has pointer-specific operators like * (dereference) and & (address-of)
```
</UniversalEditor>

## Namespace Concept

C++ uses namespaces to organize code and prevent naming conflicts, similar to how modules or imports work in JavaScript.

<UniversalEditor title="Namespace Comparison" compare={true}>
```javascript !! js
// JavaScript: Modules (similar to namespaces)
// file: math.js
export const PI = 3.14;
export function add(a, b) { return a + b; }

// file: main.js
import { PI, add } from './math.js';
console.log(PI);
console.log(add(1, 2));
```

```cpp !! cpp
// C++: Namespaces
#include <iostream>

namespace Math {
  const double PI = 3.14;
  int add(int a, int b) { return a + b; }
}

// In main function or another scope:
// std::cout << Math::PI << std::endl;
// std::cout << Math::add(1, 2) << std::endl;

// Using directive (brings all names into current scope)
// using namespace Math;
// std::cout << PI << std::endl;
```
</UniversalEditor>

---

### Practice Questions:
1.  Explain the difference between static and dynamic typing, providing examples from both JavaScript and C++.
2.  Write a C++ program that takes a user's age as input and prints whether they are a minor, an adult, or a senior citizen, using `if/else if/else` statements.
3.  How do namespaces help in C++ programming? Provide a simple example.

### Project Idea:
*   Create a simple command-line program in C++ that calculates the area of different shapes (circle, rectangle, triangle) based on user input, demonstrating function usage and basic data types.
